Search this book | Previous | Table of contents | Next

Global variables and ISINDEX revisited


The global client variables and the use of ISINDEX are still supported in scripts called via AppleEvents.

Just because scripts saved as CGI or ACGI files are more efficient than .script files, does not mean you have to unlearn everything about .script files. In fact, much of what you learned there can be applied here.

The first thing to understand is the continued existence of the client variables. The only difference is they are passed along to your script in a different manner; the client variables are passed to the script via AppleEvents rather than being prepended to your scripts as declared global variables. Furthermore, if you want to use any of the variables in your scripts, then you have to declare them yourself.

The following script (hello-world-06.cgi) demonstrates how to do this:

-- process the AppleEvent sent to this script by the server
on «event WWW*sdoc» path_args given «class kfor»:http_search_args, «class user»:username, «class pass»:password, «class frmu»:from_user, «class addr»:client_address, «class post»:post_args, «class meth»:method, «class svnm»:server_name, «class svpt»:server_port, «class scnm»:script_name, «class ctyp»:content_type, «class refr»:referer, «class Agnt»:user_agent
	
	-- define the standard HTTP header
	set LF to ASCII character (10)
	set CR to return
	set CRLF to CR & LF
	set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬
		"Server: MacHTTP" & CRLF & ¬
		"MIME-Version: 1.0" & CRLF & ¬
		"Content-type: text/html" & CRLF & CRLF
	
	-- return the results, including the prepended variables, as an HTML file
	return http_10_header & ¬
		"<html>" & ¬
		"<head>" & ¬
		"<title>Hello, World</title>" & ¬
		"</head>" & ¬
		"<body>Hello, World!" & ¬
		"<p>http_search_args: " & http_search_args & ¬
		"<br>path_args: " & path_args & ¬
		"<br>post_args: " & post_args & ¬
		"<br>method: " & method & ¬
		"<br>client_address: " & client_address & ¬
		"<br>username:" & username & ¬
		"<br>password: " & password & ¬
		"<br>from_user:" & from_user & ¬
		"<br>server_name:" & server_name & ¬
		"<br>server_port:" & server_port & ¬
		"<br>script_name: " & script_name & ¬
		"<br>content_type: " & content_type & ¬
		"<br>referer: " & referer & ¬
		"<br>user_agent: " & user_agent & ¬
		"</body>" & ¬
		"</html>"	
end «event WWW*sdoc»
The most important line in this script is the one waiting for the WWW*sdoc AppleEvent. As described in the previous section, this line is activated when your server launches the script and sends the AppleEvent. This AppleEvent brings with it a number of parameters. These parameters represent the same variables prepended to .script files.

In order to use these parameters in your scripts, you must extract them from the AppleEvent. The path arguments of a URL (everything following the dollar sign and before the question mark) are included in the direct parameter of the AppleEvent. To use the path arguments in our script, simply put a variable (like path_args) after the on «event WWW*sdoc» statement.

The other variables (the indirect parameters) must by extracted using a combination of the given command and AppleEvent keyword codes. The keyword codes for the remaining client variables are:

Thus, if you wanted to use the client variable frmu (from user) in your script, then you would have to assign it to a variable in the on «event WWW*sdoc» line something like this:
 on «event WWW*sdoc» given «class frmu»:from_user 
Consequently, the variable from_user would containing the name of the person at the other end of the client program. Similarly, if you wanted to extract the referer information (the URL of the page calling your script), then you would put something like this on the on «event WWW*sdoc» line:
on «event WWW*sdoc» given «class refr»:referer

ISINDEX tags still work in CGI/ACGI scripts too. As the new, CGI version of the Name Game script (name-game.cgi) demonstrates:

-- process the AppleEvent sent to this script by the server
on «event WWW*sdoc» given «class kfor»:http_search_args
	
	-- define the standard HTTP header
	set LF to ASCII character (10)
	set CR to return
	set CRLF to CR & LF
	set http_10_header to "HTTP/1.0 200 OK" & CRLF & ¬
		"Server: MacHTTP" & CRLF & ¬
		"MIME-Version: 1.0" & CRLF & ¬
		"Content-type: text/html" & CRLF & CRLF
	
	if http_search_args = "" then
		
		-- http_search_args is empty; request input via ISINDEX
		return http_10_header & ¬
			"<html><head>" & ¬
			"<title>Play the Name Game</title><isindex></head>" & ¬
			"<body>" & ¬
			"<H1>Play the Name Game</H1>" & ¬
			"</body></html>"		
	else
		
		-- get our name
		set theName to word 1 of http_search_args as string
		
		-- initalize some variables for the next routine
		set foundVowel to 0
		set theCounter to 0
		set theSuffix to theName
		
		-- work through the name until we find the first vowel
		repeat until foundVowel is greater than 0
			
			-- increment our counter
			set theCounter to theCounter + 1
			
			-- get the next character
			set theCharacter to the first item of theSuffix
			
			-- check whether or not it is a vowel
			if "aeiouy" contains theCharacter or "AEIOUY" contains theCharacter then
				
				-- found it, exit
				set foundVowel to 1
				
			else
				
				-- left-hand truncate the name
				set theSuffix to items 2 through length of theSuffix as string
			end if
			
		end repeat
		
		-- compose the ryhmes
		set ryhmeOne to "B" & theSuffix
		set ryhmeTwo to "F" & theSuffix
		
		-- return the lyrics
		return http_10_header & ¬
			"<html><head>" & ¬
			"<title>The Name Game</title>" & ¬
			"<body>" & ¬
			"<h1>The Name Game</h1>" & ¬
			"<blockquote>" & theName & ", " & theName & ", Bo " & ryhmeOne & ".<br>" & return & ¬
			"Bananna fana, Fo " & ryhmeTwo & ".<br>" & return & ¬
			"Fe Fi Fo, " & ryhmeTwo & ".<br>" & return & ¬
			"<strong>" & theName & "</strong>!" & ¬
			"</body>" & ¬
			"</html>"		
	end if
	
end «event WWW*sdoc»
This new version of the Name Game begins by waiting for the AppleEvent from the server. When the AppleEvent is received, the search arguments (everything after the question mark of a URL) are assigned to the variable http_search_args. This is because all WWW clients append a question mark and the value of the ISINDEX field after the URL. The script then continues as it did in its .script version by evaluating the contents of http_search_args and branching accordingly.

The point of this section is two-fold. First, client variables discussed in the .scripts sections of this book are still available to you in CGI/ACGI applications. Using AppleScript, you must extract these variables from the AppleEvents manually instead of relying on the server to prepend them to our scripts.

Second, the use of ISINDEX tags to create simple text fields continues to be a valid method for getting input.


Search this book | Previous | Table of contents | Next

Eric last edited this page on September 26, 1995. Please feel free to send comments.